package loteria.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import loteria.ItemUsuarioNumero;
import loteria.dao.LoteriaDAO;
import loteria.dao.LoteriaDAOFactory;
import loteria.utils.Propiedades;

/**
 * Servlet implementation class SorteoServlet
 */
public class SorteoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public SorteoServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("/");
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		LoteriaDAO dao = null;
		try{
			
		
		String nombre = request.getParameter("nombre");
		String numero = request.getParameter("numero");
		String cantidad = request.getParameter("cantidad");
		
		dao = LoteriaDAOFactory.getInstance().getDAO(Propiedades.getValue("database"));
		
		ItemUsuarioNumero iun = new ItemUsuarioNumero(nombre, Integer.parseInt(numero), Integer.parseInt(cantidad));
		
			dao.addUsuarioNumero(iun);	
			RequestDispatcher dispatcher = request.getRequestDispatcher("infoUsuario.jsp");
			request.setAttribute("dao", dao);
			dispatcher.forward(request, response);
		}catch(Exception e){
			RequestDispatcher dispatcher = request.getRequestDispatcher("/");
			dispatcher.forward(request, response);
		}
		
		
		
	}

}

